package com.hellobike.haxing.modules.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.constant.CommonConstant;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.operate.entity.OperateTiling;
import com.hellobike.haxing.modules.operate.mapper.OperateTilingMapper;
import com.hellobike.haxing.modules.operate.service.IOperateTilingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 平铺广告
 * @Author: haxing-boot
 * @Date:   2020-02-17
 * @Version: V1.0
 */
@Service
public class OperateTilingServiceImpl extends ServiceImpl<OperateTilingMapper, OperateTiling> implements IOperateTilingService {
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public Result<?> updateOperate(OperateTiling operateTiling) {
        // 1<=beginTime < 3
        QueryWrapper<OperateTiling> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(OperateTiling::getBeginTime,operateTiling.getBeginTime()).gt(OperateTiling::getEndTime,operateTiling.getBeginTime()).notIn(OperateTiling::getId,operateTiling.getId());
        int count =  super.count(queryWrapper);
        if(count>0){
            return Result.error("编辑失败;开始时间与数据库已存在时间重叠!");
        }
        // 1<=endTime < 3
        QueryWrapper<OperateTiling> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().le(OperateTiling::getBeginTime,operateTiling.getEndTime()).gt(OperateTiling::getEndTime,operateTiling.getEndTime()).notIn(OperateTiling::getId,operateTiling.getId());
        int count2 =  super.count(queryWrapper);
        if(count2>0){
            return Result.error("编辑失败;结束时间与数据库已存在时间重叠!");
        }
        // 1<=statrTime && endTime < 3
        QueryWrapper<OperateTiling> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lambda().le(OperateTiling::getBeginTime,operateTiling.getBeginTime()).gt(OperateTiling::getEndTime,operateTiling.getEndTime()).notIn(OperateTiling::getId,operateTiling.getId());
        int count3 =  super.count(queryWrapper);
        if(count3>0){
            return Result.error("编辑失败;开始时间和结束时间与数据库已存在时间重叠!");
        }
        baseMapper.updateById(operateTiling);
        redisUtil.del(CacheConstant.OPERATE_TILING_LIST);
        return Result.ok("编辑成功!");
    }

    @Override
    public Result<?> saveOperate(OperateTiling operateTiling) {
        // 1<=beginTime < 3
        QueryWrapper<OperateTiling> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(OperateTiling::getBeginTime,operateTiling.getBeginTime()).gt(OperateTiling::getEndTime,operateTiling.getBeginTime());
        int count =  super.count(queryWrapper);
        if(count>0){
            return Result.error("添加失败;开始时间与数据库已存在时间重叠!");
        }
        // 1<=endTime < 3
        QueryWrapper<OperateTiling> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().le(OperateTiling::getBeginTime,operateTiling.getEndTime()).gt(OperateTiling::getEndTime,operateTiling.getEndTime());
        int count2 =  super.count(queryWrapper);
        if(count2>0){
            return Result.error("添加失败;结束时间与数据库已存在时间重叠!");
        }
        // 1<=statrTime && endTime < 3
        QueryWrapper<OperateTiling> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lambda().le(OperateTiling::getBeginTime,operateTiling.getBeginTime()).gt(OperateTiling::getEndTime,operateTiling.getEndTime());
        int count3 =  super.count(queryWrapper);
        if(count3>0){
            return Result.error("添加失败;开始时间和结束时间与数据库已存在时间重叠!");
        }
        baseMapper.insert(operateTiling);
        redisUtil.del(CacheConstant.OPERATE_TILING_LIST);
        return Result.ok("添加成功！");
    }

}
